package suncertify.shared;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author jshah
 * Configuration screen which lets the user set parameters for RMI
 */
public class ConfigUI{

   
    JFrame clientConfigFrame;
    JPanel formPanel;
    JPanel defaultPanel;
    JTextField remoteUrlField;
    JLabel remoteUrlField_label;

 
    JLabel rmiPort_label;
    JTextField rmiPort_field;
    JLabel rmiHost_label;
    JTextField rmiHost_field;
    JButton submitButton;
    JButton defaultButton;
    String defaultUrl ;
    String defaultPort ;
    ActionListener handler;

    private static final int WIDTH = 500;
    private static final int HEIGHT = 200;

    private Properties P;
    /**
     * Constructor for ConfigUI, takes an ActionHandler Instance which handles all the events generated in this screen as an
     * argument
     * @param handler <code>ActionHandler</code> instance which handles all the events
     */
    public ConfigUI( ActionListener handler){


        this.handler = handler;
       
        this.clientConfigFrame = new JFrame("RMI SERVER CONFIGURATION");


        this.clientConfigFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.clientConfigFrame.getContentPane().setLayout(new BorderLayout());
        this.clientConfigFrame.add(this.getForm(),BorderLayout.NORTH);
        this.clientConfigFrame.add(new JLabel("OR"),BorderLayout.CENTER);
        this.clientConfigFrame.add(this.getDefaultPanel(),BorderLayout.SOUTH);
        this.clientConfigFrame.pack();
        
        this.clientConfigFrame.setVisible(true);
        this.clientConfigFrame.setResizable(false);



    }

    JPanel getForm (){

        this.formPanel = new JPanel(new GridBagLayout());
        Dimension d = new Dimension(this.WIDTH,this.HEIGHT);
        this.formPanel.setBorder(BorderFactory.createTitledBorder("RMI URL CONFIG"));

        this.formPanel.setPreferredSize(d);
        GridBagConstraints c = new GridBagConstraints();
        int anchor = GridBagConstraints.EAST;
        this.rmiHost_label = new JLabel("RMI SERVER HOST:");
        this.rmiHost_field = new JTextField(18);
        this.setMyConstraints(c, 0, 0, anchor);
        this.formPanel.add(this.rmiHost_label,c);
        this.setMyConstraints(c, 1, 0, anchor);
        this.formPanel.add(this.rmiHost_field,c);
        this.remoteUrlField_label = new JLabel("RMI SERVER URL:");
        this.remoteUrlField = new JTextField(18);
        this.setMyConstraints(c, 0, 2, anchor);
        this.formPanel.add(this.remoteUrlField_label,c);
        this.setMyConstraints(c, 1, 2, anchor);
        this.formPanel.add(this.remoteUrlField,c);
        this.rmiPort_label = new JLabel("RMI PORT:");
        this.rmiPort_field = new JTextField(4);
        this.setMyConstraints(c, 0, 4, anchor);
        this.formPanel.add(this.rmiPort_label,c);
        this.setMyConstraints(c, 1, 4,anchor);
        this.formPanel.add(this.rmiPort_field,c);
      
        
        this.submitButton = new JButton("START");
        this.submitButton.addActionListener(this.handler);
        
        
        this.setMyConstraints(c, 0,6, anchor);
        this.formPanel.add(new JLabel("SET NEW URL AND PORT"),c);
        this.setMyConstraints(c, 1,6, anchor);
        c.fill = 0;
        this.formPanel.add(this.submitButton,c);
        return this.formPanel;
    }

    JPanel getDefaultPanel(){

        this.P = new Properties();
        try {
            P.load(new FileInputStream("suncertify.properties"));
        } catch (IOException ex) {
            Logger.getLogger(ConfigUI.class.getName()).log(Level.SEVERE, "ERROR LOADING PROPS FILE", ex);
        }

        this.defaultUrl = P.getProperty("serverRmiUrl");
        this.defaultPort = P.getProperty("serverRmiPort");
        
        this.defaultPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        this.defaultPanel.setPreferredSize(new Dimension(500,100));
        this.defaultPanel.setBorder(BorderFactory.createTitledBorder("SELECT PREVIOUS"));
        int anchor = GridBagConstraints.EAST;
        this.setMyConstraints(c,0,0, anchor);
        this.defaultPanel.add(new JLabel(" URL:" + this.defaultUrl),c);
        this.setMyConstraints(c, 1, 0, anchor);
        this.defaultPanel.add(new JLabel(" Port:" + this.defaultPort),c);
        this.submitButton.setSize(5, 2);
       
        this.defaultButton = new JButton("START");
        this.defaultButton.addActionListener(this.handler);
        
        this.setMyConstraints(c, 2, 0, anchor);
        c.fill = 0;
        this.defaultPanel.add(this.defaultButton,c);
        return this.defaultPanel;
    }

     private void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) {
        c.gridx  = gridx;
        c.gridy  = gridy;
        c.anchor = anchor;
        c.insets = new Insets(5, 5, 0, 5);
        c.fill   = GridBagConstraints.HORIZONTAL;
        
    }


    /**
     * Get the handle for JFrame representing DB Config screen
     * @return JFrame returns a JFrame for configuration screen
     */
    public JFrame getClientConfigFrame() {
        return clientConfigFrame;
    }

    JButton getDefaultButton() {
        return defaultButton;
    }

    JPanel getFormPanel() {
        return formPanel;
    }

    ActionListener getHandler() {
        return handler;
    }

    String getPort() {
        return defaultPort;
    }

    JTextField getRemoteUrlField() {
        return remoteUrlField;
    }

    JLabel getRemoteUrlField_label() {
        return remoteUrlField_label;
    }

    JTextField getRmiHost_field() {
        return rmiHost_field;
    }

    JLabel getRmiHost_label() {
        return rmiHost_label;
    }

    JTextField getRmiPort_field() {
        return rmiPort_field;
    }

    JLabel getRmiPort_label() {
        return rmiPort_label;
    }

    JButton getSubmitButton() {
        return submitButton;
    }

    String getUrl() {
        return defaultUrl;
    }


}
